package com.zhang.set;


import com.zhang.map.HashMap;
import com.zhang.map.Map;

/**
 * @author 张吉鑫
 * @Description 实现HashSet集合
 * @date 2021/4/4 19:08
 */
public class HashSet<E> implements Set<E>{
    private HashMap<E,Object> map = new HashMap<>();
    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public void clear() {
        map.clear();
    }

    @Override
    public boolean contains(E element) {
        return map.containsKey(element);
    }

    @Override
    public void add(E element) {
        map.put(element,null);
    }

    @Override
    public void remove(E element) {
        map.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if (visitor == null){
            return;
        }
        map.traversal(new Map.Visitor<E,Object>() {

            @Override
            public boolean visit(E key, Object value) {
                return visitor.visit(key);
            }
        });
    }
}
